<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function sum(a, b, c) {
        return a + b + c;
      }
      function curry(fn, ...args) {
        let length = fn.length;
        args = args || [];
        return function () {
          let subArgs = args.slice(0);
          // 合并所有的参数
          subArgs = [...subArgs, ...arguments];
          // 判断参数的长度是否已经满足函数所需要的长度
          if (subArgs.length >= length) {
            // 拿到执行完函数的值
            return fn.apply(this, subArgs);
          } else {
            // 返回柯里化化函数，等待继续传递参数
            return curry.call(this, fn, ...subArgs);
          }
        };
      }
      let add = curry(sum);
      console.log(add(1)(1, 2, 3)); // 6
      console.log(add(1)(2)(3)); // 6
      console.log(add(1, 2, 3)); // 6
      console.log(add()(1, 2, 3)); // 6

      function curry1(fn, ...args) {
        return fn.length <= args.length ? fn(...args) : curry1.bind(null, fn, ...args);
      }
      let add1 = curry1(sum);
      // console.log(add1(1)(2, 3));
      // console.log(add(1, 2, 3));
      // console.log(curry1(sum, 1, 2, 3));
      function fn(a, b, c) {
        console.log(a, b, c);
      }
      let fn1 = fn.bind(null, 1, 2);
      fn1(3);
    </script>
  </body>
</html>
